<template>
  <div class="userCollection-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>我的收藏</span>
        </div>
      </template>

      <el-table
        v-loading="loading"
        :data="collections"
        border
        style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="80" />
        <el-table-column
          prop="activityName"
          label="活动名称">
          <template #default="{ row }">
            <router-link
              :to="`/activity/detail/${row.activityId}`"
              class="link-type">
              {{ row.activityName }}
            </router-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="activityType"
          label="活动类型"
          width="120">
          <template #default="{ row }">
            {{ activityTypeMap[row.activityType] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="activityStatus"
          label="活动状态"
          width="120">
          <template #default="{ row }">
            <el-tag :type="getActivityStatusType(row.activityStatus)">
              {{ activityStatusMap[row.activityStatus] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="收藏时间"
          width="180" />
        <el-table-column
          label="操作"
          width="150">
          <template #default="{ row }">
            <el-button
              type="danger"
              size="small"
              @click="handleCancelCollection(row)">
              取消收藏
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="queryParams.current"
          v-model:page-size="queryParams.size"
          :page-sizes="[10, 20, 30, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useCollectionStore } from '@/stores/userCollection'
import { ElMessage, ElMessageBox } from 'element-plus'

const collectionStore = useCollectionStore()
const loading = ref(false)
const collections = ref([])
const total = ref(0)
const queryParams = reactive({
  current: 1,
  size: 10
})

// 活动类型映射
const activityTypeMap = {
  0: '校内活动',
  1: '校外活动'
}

// 活动状态映射
const activityStatusMap = {
  0: '未开始',
  1: '进行中',
  2: '已结束',
  3: '已取消'
}

// 根据状态获取标签类型
const getActivityStatusType = (status) => {
  switch (status) {
    case 0: return 'info'
    case 1: return 'success'
    case 2: return 'warning'
    case 3: return 'danger'
    default: return 'info'
  }
}

// 获取收藏列表
const getCollectionList = async () => {
  loading.value = true
  try {
    const response = await collectionStore.getUserCollections(queryParams)
    collections.value = response.records || []
    total.value = response.total || 0
  } catch (error) {
    console.error('获取收藏列表失败', error)
    ElMessage.error('获取收藏列表失败')
  } finally {
    loading.value = false
  }
}

// 取消收藏
const handleCancelCollection = (row) => {
  ElMessageBox.confirm('确定要取消收藏该活动吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await collectionStore.cancelCollection(row.activityId)
      ElMessage.success('取消收藏成功')
      getCollectionList()
    } catch (error) {
      console.error('取消收藏失败', error)
      ElMessage.error('取消收藏失败')
    }
  }).catch(() => {
    // 用户取消操作
  })
}

// 处理页码改变
const handleCurrentChange = (current) => {
  queryParams.current = current
  getCollectionList()
}

// 处理每页条数改变
const handleSizeChange = (size) => {
  queryParams.size = size
  getCollectionList()
}

onMounted(() => {
  getCollectionList()
})
</script>

<style scoped>
.userCollection-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}

.link-type {
  color: #409EFF;
  text-decoration: none;
}

.link-type:hover {
  text-decoration: underline;
}
</style>
